// pages/user/user.js
//包含 MD5文件提供加密方法
var md5Js = require('../../utils/md5.js');
var utils = require('../../utils/util.js');
// console.log(md5Js.hex_md5('123')); //md5 字符串
var app = getApp();
var object = {};
object.data = {
  user_infos :[]
};
//执行登录方法
object.formSubmit = function(e){
  wx.clearStorageSync();
   var req = e.detail.value;
   var user = [];
   for(var i in req){
     if(!req[i]){
         wx.showModal({
            title:'提示',
            content:'账号密码不能为空',
            showCancel:false,
            confirmColor:'black'
         })
        return false;
     }
     user.push(req[i].trim());
   }
  if(user.length==2){
      // console.log(utils.isEmptyObject({res:1}));return;
      var username = user[0];
      var userpass = user[1];
      var t = new Date().getTime().toString();
      //console.log(t)
      var private_key = md5Js.hex_md5(userpass);
      console.log('userlogin=>'+private_key);
      var k = md5Js.hex_md5(private_key+username+t);
      var parms_url  = app.request_url+'UserLogin?';
          parms_url += 'account='+username;
          parms_url += '&t='+t;
          parms_url += '&k='+k;
          parms_url += '&platform=IOS&uuid=123456789';
          parms_url += '&device=IOS:iPhone(10.2)&uuid=35396F6421CD495ABD6FEF1F38995CBD&v='+app.version
          console.log(parms_url);
          wx.request({
            url: parms_url,
            method: 'GET',
            success: function(res){
              var respones = res;
              if((respones.statusCode=='200')&&(respones.data.Success==1)){
                //登录成功
                  var user_items = respones.data.CallInfo;
                  //d调用异步存储api将用户信息写进缓存
                  wx.setStorage({
                    key: 'user_info',
                    data: {'user_itmes':user_items,private_key:private_key},
                    success: function(res){
                      wx.showToast({
                        title:'登录成功',
                        icon:'loading'
                      });
                      setTimeout(function(){
                          wx.switchTab({
                            url: '../index/index',
                          })
                      },1000)
                    },
                  })
                //获取支付配置信息
                  if(respones.data.CallInfo){
                    var request_data = respones.data.CallInfo;
                    var uid         = request_data.id.toString();
                    var t           =  new Date().getTime().toString();
                    var k           = md5Js.hex_md5(private_key+t);//key
                    var parms_url   = app.request_url+'C_PayConfig/?';
                    parms_url += 'uid='+request_data.id;
                    parms_url += '&userType='+request_data.type;
                    parms_url += '&t='+t;
                    parms_url += '&k='+k;
                    parms_url += '&v='+app.version;
                    //调用产品页接口
                    wx.request({
                      url: parms_url,
                      method: 'GET',
                      success: function(res){
                        var respones = res;
                        //请求成功分配模板变量
                        if((respones.statusCode=='200')&&(respones.data.Success==1)){
                          //调用API从本地缓存中获取数据
                          var logs = wx.getStorageSync('logs') || []
                          var app_id = respones.data.CallInfo.APPID;
                          var app_secret = respones.data.CallInfo.APPSECRET;
                          // var app_id = 'wxe59a8e4c7499a226'
                          // var app_secret = 'cbb75da5593d6c6f2be383ce2f3b5dee'
                          logs.unshift(Date.now())
                          wx.setStorageSync('logs', logs)
                          var that = this;
                            //调用登录接口
                            console.log(111111);
                            wx.login({
                              success: function (log) {
                                if (log.code){
                                  wx.setStorageSync('code', log.code)
                                  var code = wx.getStorageSync('code');
                                  wx.request({
                                    url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+app_id+'&secret='+app_secret+'&js_code='+code+'&grant_type=authorization_code', //仅为示例，并非真实的接口地址
                                    method:'GET',
                                    data: {},
                                    header: {
                                        'content-type': 'application/json'
                                    },
                                    success: function(opres) {
                                      wx.setStorageSync('openID', opres.data.openid)
                                      //that.globalData.openid = opres.data.openid;
                                    }
                                  });
                                    
                                } else {
                                  console.log('获取用户登录态失败！' + res.errMsg);
                                }
                                
                              }
                            })
               
                        }
                      }
                    })
                  }
            }else{
                wx.showModal({
                  title:'提示',
                  content:'检查你的账户密码！',
                  showCancel:false,
                  confirmColor:'black'
                })    
            }
        },
        fail: function() {
          wx.showModal({
            title:'提示',
            content:'网络异常请求失败!',
            showCancel:false,
            confirmColor:'black'
         })    
        },
      })
  }
};

object.onLoad  = function(options){
  //页面首次进来加载调用的方法
};
object.onReady = function(){
  //页面加载完成调用的方法
};
object.onShow  = function(){
  //从后台进入前台显示页面调用
};
object.onHide = function(){
  //从前台进入后台隐藏页面调用
};
object.onUnload = function(){
  //页面关调用
} 
//分配模板变量
Page(object);